<?php
class user_type { 
    var $ut_id;
    var $ut_desc;
    var $ut_status;


    function Delete(){
        
        include_once 'recursos/conexionMysql.php';
        $DB = new conexionMysql();
        $DB->ConectarBD();
        $q = " select ut_status from user_type where ut_id ='".$this->ut_id."' " ;
        $rse = $DB->ObtenerData($q);   
        
        if($rse[0]["ut_status"]==0){
            $this->ut_status = 1;
        }else{
            $this->ut_status = 0;
        }
        
        $query = " UPDATE estado SET
                     ut_status='".$this->ut_status."'
                     where ut_id ='".$this->ut_id."' " ;
        
        $rs = $DB->EjecutarQuery($query);        
        $DB->DesconectarBD();        
        return $rs;  
    }

    function Update(){
        include_once 'recursos/conexionMysql.php';
        $DB = new conexionMysql();
        $DB->ConectarBD();
        
        $query = " " ;
        
        $rs = $DB->EjecutarQuery($query);        
        $DB->DesconectarBD();        
        return $rs;  
    }

    function GetData(){
        include_once 'recursos/conexionMysql.php';
        $DB = new conexionMysql();
        $DB->ConectarBD();
         
        $query = " Select * from user_type where ut_id ='".$this->ut_id."' ";
        $rs = $DB->ObtenerData($query, "user_type" );

        $this->ut_id = $rs[0]["ut_id"];
        $this->ut_desc = $rs[0]["ut_desc"];
        $this->ut_status = $rs[0]["ut_status"];

        $DB->DesconectarBD();        
    }

    function Save(){
        include_once 'recursos/conexionMysql.php';
        $DB = new conexionMysql();
        $DB->ConectarBD();
        $query = " INSERT INTO user_type (
                     ut_id, ut_desc, ut_status)
                    VALUES (NULL, '$this->ut_desc', '$this->ut_status');" ;
        
        $rs = $DB->EjecutarQuery($query);        
        $DB->DesconectarBD();        
        return $rs;        
    }
    
    function GetList($vals = false, $orderby = false, $lim= false){
        include_once 'recursos/conexionMysql.php';
        $DB = new conexionMysql();
        $DB->ConectarBD();
        
        if ($_REQUEST["seccion"] == "usertype" ) { //modulo user_type
        
            if(!isset($_REQUEST["li"])){
                $li = 0;
            }else{
                $li = $_REQUEST["li"];
            }               

            if(isset($_REQUEST["page"])){
                $li = ($_REQUEST["page"]* $_SESSION['PREF']['LIST_LIMIT'])- $_SESSION['PREF']['LIST_LIMIT'];
            }

            if(!$vals){
                $vals = " * ";            
            }

            if(!$lim){
                $limit ="LIMIT ".$li.", ".$_SESSION['PREF']['LIST_LIMIT']." ";
            }

            if($orderby){            
                $order =" ORDER BY $orderby ";
            }else{
                $order =" ORDER BY ut_status desc, ut_desc asc ";
            }

            if(isset($_REQUEST["estado"]) && $_REQUEST["estado"] <> 99 ){ 
                $where .= " and ut_status = ".$_REQUEST["estado"]."  ";
            }   

            if($_REQUEST["filtro"]){
                $query = " Select $vals from user_type where ut_desc like '%".$_REQUEST["filtro"]."%' $where
                                $order  $limit ";
            }else{
                $query = " Select $vals from user_type where 1=1 $where   $order  $limit   ";
            }
            
        } else {  //desde fuera del MODULO USER_TYPE
            
             $query = " Select $vals from user_type where ut_status = 1 $where  ";
            
        }
        
        $rs = $DB->ObtenerData($query, "user_type");

        if (trim($vals) != "count(*)"){

            foreach ($rs as $key => $val) {
                $u = new user_type();
                $u->set_ut_id($val["ut_id"]);
                $u->set_ut_desc($val["ut_desc"]);
                $u->set_ut_status($val["ut_status"]);
                $rs2[] = $u;
            }

            $rs = $rs2;

            include_once 'recursos/funciones.php';
            $e = getEstado();        
            if(is_array($rs)){
                foreach ($rs as $key => $value) {
                    if ($value["ut_status"] == 1) {
                        $rs[$key]["ut_status"] = "<img src='images/png/estado_habilitado.png' />".$e[$value["ut_status"]];
                    } else {
                        $rs[$key]["ut_status"] = "<img src='images/png/estado_deshabilitado.png' />".$e[$value["ut_status"]];
                    }  
                }
            }
        }
        $DB->DesconectarBD();      
        return $rs;                
    }

    function get_ut_id(){
        return $this->ut_id;
    }
    function set_ut_id($val){
        $this->ut_id = $val;
    }

    function get_ut_desc(){
        return $this->ut_desc;
    }
    function set_ut_desc($val){
        $this->ut_desc = $val;
    }

    function get_ut_status(){
        return $this->ut_status;
    }
    function set_ut_status($val){
        $this->ut_status = $val;
    }

}